// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FavouritesViewModel.cs" company="XamlmNinja">
//   2010
// </copyright>
// <summary>
//   The favourites view model.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ServiceStyleRatherThanMessengerStyle.ViewModel
{
    using System;
    using System.Threading;

    using GalaSoft.MvvmLight.Messaging;

    using ServiceStyleRatherThanMessengerStyle.Model;
    using ServiceStyleRatherThanMessengerStyle.Services;
    using ServiceStyleRatherThanMessengerStyle.ViewModel.Messages;

    using WP7Contrib.Collections;
    using WP7Contrib.Logging;
    using WP7Contrib.Services.Navigation;
    using WP7Contrib.Services.Storage;

    /// <summary>
    /// The favourites view model.
    /// </summary>
    public class FavouritesViewModel : ViewModelBaseWp7
    {
        #region Constants and Fields

        /// <summary>
        /// The favourites service.
        /// </summary>
        private readonly IFavouritesService favouritesService;

        /// <summary>
        /// The currently selected person.
        /// </summary>
        private Person currentlySelectedPerson;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="FavouritesViewModel"/> class.
        /// </summary>
        /// <param name="navigationService">
        /// The navigation service.
        /// </param>
        /// <param name="messenger">
        /// The messenger.
        /// </param>
        /// <param name="log">
        /// The log.
        /// </param>
        /// <param name="favouritesService">
        /// The favourites service.
        /// </param>
        public FavouritesViewModel(
            INavigationService navigationService, IMessenger messenger, ILog log, IFavouritesService favouritesService)
            : base(navigationService, messenger, log)
        {
            this.favouritesService = favouritesService;

            ThreadPool.QueueUserWorkItem(
                state => { this.MessengerInstance.Register<SelectedPersonMessage>(this, this.OnReceiveMessage); });
        }

        #endregion

        /// <summary>
        /// Gets CurrentlySelectedCurrentlySelectedPerson.
        /// </summary>
        public Person CurrentlySelectedPerson
        {
            get
            {
                return this.currentlySelectedPerson;
            }

            private set
            {
                this.SetPropertyAndNotify(ref this.currentlySelectedPerson, value, "CurrentlySelectedPerson");
            }
        }


        public ReadOnlyObservableCollection<Person> Favourites
        {
            get
            {
                return this.favouritesService.Favourites;
            }
        }

        #region Methods

        private void OnReceiveMessage(SelectedPersonMessage obj)
        {
            if (obj == null)
            {
                return;
            }

            if (obj.Person == null)
            {
                return;
            }

            this.CurrentlySelectedPerson = obj.Person;
        }

        /// <summary>
        /// The is being activated.
        /// </summary>
        /// <param name="storage">
        /// The storage.
        /// </param>
        protected override void IsBeingActivated(IStorage storage)
        {
        }

        /// <summary>
        /// The is being deactivated.
        /// </summary>
        /// <param name="storage">
        /// The storage.
        /// </param>
        protected override void IsBeingDeactivated(IStorage storage)
        {
        }

        #endregion
    }
}